﻿using ServiceReference1;
using System;
using System.Collections;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using XXX.Bo;
using XXX.Common;
using XXX.Models.AS;
using ZDEntity;

namespace U8COWeb
{
    /// <summary>
    /// 存货档案单据接口
    /// </summary>
    public static class VendorDALCO
    {
 

        public static bool AddVendor(AS_Vendor rdrecord, string xmlconfig, out string err, out string id)
        {
            try
            {
                string xdomhead = XmlOperation.ObjectToXmlSerializer(rdrecord);
                XDocument xdoc = XDocument.Parse(xdomhead);

               
                // 检查根元素是否为 AS_Inventory 并改名
                if (xdoc.Root.Name.LocalName == "AS_Vendor")
                {
                    // 更改根元素的名称
                    xdoc.Root.Name = "Vendor";
                }
                // 如果需要，可以将修改后的 XML 转换成字符串
                string updatedXmlString = xdoc.ToString();
                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("AS", "vendor", "add", "", updatedXmlString, "", "", xmlconfig, "config").Result;
                string ccode = "";
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "AS");
                sb.AppendLine("cVouchType=" + "vendor");
                sb.AppendLine("cOptType=" + "add");
                sb.AppendLine("cVouchID=" + "");
                sb.AppendLine("domHead=" + updatedXmlString);
                sb.AppendLine("CO=" + result.ToString());
                ExLog.WriteTxt("CO", sb.ToString());
                return XmlOperation.GetCoResult("供应商档案新增", "0", out err, out id, out ccode, result);

            }
            catch (Exception ex)
            {
                id = "";
                err = ex.Message;
                return false;
            }
        }

        public static bool ModifyVendor(AS_Vendor rdrecord, string xmlconfig, out string err)
        {
            try
            {
                string xdomhead = XmlOperation.ObjectToXmlSerializer(rdrecord);
                XDocument xdoc = XDocument.Parse(xdomhead);


                // 检查根元素是否为 AS_Inventory 并改名
                if (xdoc.Root.Name.LocalName == "AS_Vendor")
                {
                    // 更改根元素的名称
                    xdoc.Root.Name = "Vendor";
                }
                // 如果需要，可以将修改后的 XML 转换成字符串
                string updatedXmlString = xdoc.ToString();

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("AS", "vendor", "modify", rdrecord.cVenCode, updatedXmlString, "", "", xmlconfig, "config").Result;
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "AS");
                sb.AppendLine("cVouchType=" + "vendor");
                sb.AppendLine("cOptType=" + "modify");
                sb.AppendLine("cVouchID=" + "");
                sb.AppendLine("domHead=" + updatedXmlString);
                sb.AppendLine("CO=" + result.ToString());
                ExLog.WriteTxt("CO", sb.ToString());
                return XmlOperation.GetCoResult("供应商档案修改", "0", out err, out _, out _, result);

            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
        }

        public static bool DelVendor(string  cVenCode, string xmlconfig, out string err)
        {
            try
            { 
                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("AS", "vendor", "delete", cVenCode, "", "", "", xmlconfig, "config").Result;
                string id = "";
                string ccode = "";
                return XmlOperation.GetCoResult("供应商档案删除", "0", out err, out id, out ccode, result);
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
        }

       

       
    }
}
